package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.dto.R;
import com.cdtu.entity.User;
import com.cdtu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author : Sakura
 * @Date : 2023/10/27 19:08
 * @Description : TODO
 */
@RestController
@RequestMapping("/api/user")
@CrossOrigin
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param
     * @return
     * @author
     * @date
     */
    @PostMapping("/login")
    public R login(@RequestBody User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        queryWrapper.eq(User::getPassword, user.getPassword());
        User one = userService.getOne(queryWrapper);
        // 判断用户是否存在
        if (one != null) {
            return R.success(one);
        } else {
            return R.error("账号或者密码错误");
        }
    }

    /**
     * 用注册
     *
     * @param
     * @return
     * @author
     * @date
     */
    @PostMapping("/register")
    public R register(@RequestBody User user) {
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        boolean save = userService.save(user);
        // 判断用户是否存在
        if (save) {
            return R.success("注册成功");
        } else {
            return R.error("账号或者密码错误");
        }
    }




}
